package com.java2007.controller.front;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.java2007.controller.BaseServlet;
import com.java2007.entity.CartItem;
import com.java2007.factory.BeanFactory;
import com.java2007.service.CartService;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 餐车控制层
 */
@WebServlet("/foodcart")
public class FoodCartController extends BaseServlet {
    private CartService cartService= (CartService) BeanFactory.getBean("com.java2007.service.impl.CartServiceImpl");
    /**
     * 修改餐车菜品数量的方法
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String updateByFoodId(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession();

        Map<String,Object> map=new HashMap<>();
        String foodIdStr = request.getParameter("foodId");
        String numStr = request.getParameter("num");
        int num= Integer.parseInt(numStr);
        List<CartItem> cart = (List<CartItem>) session.getAttribute("front_cart");
        if (cart.size()<=0 || cart==null){
            return "<script>alert('不用改');</script>";
        }
        int foodId= Integer.parseInt(foodIdStr);
        for (CartItem cartItem : cart) {
            if (cartItem.getFoodId()==foodId){
                cartItem.setNum(num);
                cartItem.setTotalPrice(cartItem.getNum()*cartItem.getFoodPrice());
                map.put("toprice",cartItem.getTotalPrice());
                cartItem.setUpdateTime(new Date());
                break;
            }
        }
        Long finalPrice=0L;
        for (CartItem cartItem:cart) {
            finalPrice+=cartItem.getTotalPrice();
        }
        //将新数据存到session
        session.setAttribute("front_cart",cart);
        session.setAttribute("final_price",finalPrice);
        //将结果存到map中，通过json发给前端
        map.put("finalprice",finalPrice);
        map.put("success",true);
        String jsonString = JSON.toJSONString(map);
        return jsonString;
    }
    /**
     * 删除餐车菜品的方法
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String deleteByFoodId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        String foodIdStr = request.getParameter("foodId");
        List<CartItem> cart = (List<CartItem>) session.getAttribute("front_cart");
        if (cart.size()<=0 || cart==null){
            return "<script>alert('没有参数，无需删除');</script>";
        }
        int foodId= Integer.parseInt(foodIdStr);
        for (CartItem cartItem : cart) {
            if (cartItem.getFoodId()==foodId){
                cart.remove(cartItem);
                break;
            }
        }
        Long finalPrice=0L;
        for (CartItem cartItem:cart) {
            finalPrice+=cartItem.getTotalPrice();
        }
        //将新数据存到session
        session.setAttribute("front_cart",cart);
        session.setAttribute("final_price",finalPrice);
        //将结果存到map中，通过json发给前端
        Map<String,Object> map=new HashMap<>();
        map.put("finalprice",finalPrice);
        map.put("success",true);
        String jsonString = JSON.toJSONString(map);
        return jsonString;
    }
    /**
     * 添加到餐车的方法
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        String foodIdStr = request.getParameter("foodId");
        String numStr = request.getParameter("num");
        if (!StringUtils.isEmpty(foodIdStr) && !StringUtils.isEmpty(numStr)){
            List<CartItem> list= (List<CartItem>)session.getAttribute("front_cart");
            if (list==null){
                list=new ArrayList<>();
            }
            int foodId= Integer.parseInt(foodIdStr);
            int num= Integer.parseInt(numStr);

            list=cartService.add(foodId,num,list);
            session.setAttribute("front_cart",list);
            //计算餐车总价钱
            Long finalPrice=0L;
            for (CartItem cartItem : list) {
                finalPrice+=cartItem.getTotalPrice();
            }
            session.setAttribute("final_price",finalPrice);
        }
        return "redirect:/front/detail/clientCart.jsp";
    }
}
